﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace CGF
{
    [AddComponentMenu("CGFramework/Switch")]
    public class Switch : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
    {
        public static event Action PlayGlobalButtonSoundAction;

        public int index;
        public GameObject on, off;
        public Text text;
        
        public float pressScale = 0.98f;
        public bool globalSound = true;
        public RectTransform rectTransform { get { return transform as RectTransform; } }
        public SwitchGroup buttonGroup { get; private set; }
        [Space]
        public Action<bool> onStateChanged;
        private Vector3 defaultScale;
        private int currPointerId;
        private bool _isOn;

        public bool isOn
        {
            get { return _isOn; }
            private set
            {
                _isOn = value;
                if (on) on.SetActive(_isOn);
                if (off) off.SetActive(!_isOn);
            }
        }

        void Awake()
        {
            if (on)
            {
                isOn = on.activeSelf;
            }else if (off)
            {
                isOn = !off.activeSelf;
            }
        }

        public void InitByGroup(SwitchGroup buttonGroup,int index)
        {
            this.buttonGroup = buttonGroup;
            this.index = index;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (!buttonGroup)
            {
                //没有buttonGroup用Click
                SetIsOn(!isOn, true);
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (enabled)
            {
                if (buttonGroup)
                {
                    //有buttonGroup，用Down，但只有关闭状态才能点
                    if(isOn)return;
                    SetIsOn(!isOn, true);
                }

                if (pressScale != 1f)
                {
                    if (defaultScale == Vector3.zero)
                    {
                        defaultScale = transform.localScale;
                    }
                    transform.localScale = defaultScale * pressScale;
                }
                currPointerId = eventData.pointerId;
            }
        }

        public void SetIsOn(bool b, bool triggerEvent)
        {
            isOn = b;
            //事件
            if (triggerEvent)
            {
                onStateChanged?.Invoke(isOn);
                if (isOn)
                {
                    buttonGroup?._OnSwitchOn(index);
                }
            }
            //声音
            if (buttonGroup)
            {
                if (isOn)
                {
                    if (globalSound) PlayGlobalButtonSoundAction?.Invoke();
                }
            }
            else
            {
                if (globalSound) PlayGlobalButtonSoundAction?.Invoke();
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            bool canExecUp = true;
            if (Application.isMobilePlatform)
            {
                canExecUp = (currPointerId == eventData.pointerId);
            }
            if (canExecUp)
            {
                currPointerId = -1;
                //变换效果恢复
                if (defaultScale != Vector3.zero) transform.localScale = defaultScale;
            }
        }

#if UNITY_EDITOR
        [UnityEditor.MenuItem("CGFramework/创建 Switch")]
        [UnityEditor.MenuItem("GameObject/CGFramework/创建 Switch", priority = -2200)]
        static void Create()
        {
            GameObject go = new GameObject("Switch");
            go.layer = LayerMask.NameToLayer("UI");
            go.transform.SetParent(UnityEditor.Selection.activeGameObject.transform, false);
            go.AddComponent<Image>();
            Switch button = go.AddComponent<Switch>();
            button.off = new GameObject("Off", typeof(RectTransform));
            button.off.transform.SetParent(go.transform, false);
            button.on = new GameObject("On", typeof(RectTransform));
            button.on.transform.SetParent(go.transform, false);
            UnityEditor.EditorUtility.SetDirty(go);
        }
#endif

    }
}